/**
 * Created by yyj on 2015/10/28.
 */

$(function(){
	
    //主机群组的左移
    $("#addHostGroup").click(function() {
    	addHostGroup();
    })
    //主机群组右移
    $("#removeHostGroup").click(function() {
    	removeHostGroup();
    })
    //主机的左移
	$("#addHost").click(function () {
		addHost();
	});
	
	//主机的右移
	$("#removeHost").click(function() {
		removeHost();
	})

    //点击关闭按钮
    $('.press_btn2.Close').click(function(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    })
    //点击保存按钮
    $('.press_btn1').click(function(){
        //alert(1)
    	var host = $("input[name='host']").val();
        var name = $("input[name='name']").val();
        
        //将被选中的groupid值传到后台
        var groupids = new Array();
        var groupidList = $("#groupids > option");
        $.each(groupidList, function(i, obj) {
        	groupids[i]=$(obj).val();
        });
        
        var hostids = new Array();
        var hostidList = $("#hostids > option");
        $.each(hostidList, function(i, obj) {
        	hostids[i]=$(obj).val();
        })
        if (host == "") {
            layer.msg("主机名称不能为空", {
                time : 1500
            });
            return;
        }
        if (groupids == null || groupids.length==0){
        	layer.msg("群组为必选项",{
        		time:1000
        	});
        	return;
        } 
        $.ajax({
            url:webPath+ "/radar/template/saveTemplate.do",
            type : "post",
            dataType:"json",
            data:{
            	"host":host,
                "name":name,
                "groupids":groupids.join(","),
                "hostids":hostids.join(",")
            },
            async:false,
            beforeSend : function() {
                $(".press_btn1").attr("disabled", "disabled");
            },
            error : function(data) {
            	alert("请求错误");
            },
            success : function(json) {
                var code = json.code;
                var result = json.errorMessage;
                if (code != null && code == "200"){
                        layer.msg(result, {time:1000},
                            function(){
                        parent.paging(1);// 调用父窗口的方法
                        var index = parent.layer.getFrameIndex(window.name);
                        setTimeout('parent.layer.close('+index+')',1001);
                    });
                }else{
                    layer.alert(result);
                }
            }
        });
    });
});

function addHost() {
	var hostids = $("#hostids");
	var hostNameList =  $("#hostNameList > option:selected");
	hostids.append(hostNameList);
}

function removeHost() {
	var hostNameList = $("#hostNameList");
	var hostids =  $("#hostids > option:selected");
	hostNameList.append(hostids);
}

function addHostGroup() {
	var groupids = $("#groupids");
	var groupNameList =  $("#groupNameList > option:selected");
	groupids.append(groupNameList);
}

function removeHostGroup() {
	var groupNameList = $("#groupNameList");
	var groupids = $("#groupids > option:selected");
	groupNameList.append(groupids);
}